using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesMyClaims : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region CurrentPageIndex
    protected int CurrentPageIndex
    {
        get
        {
            return (int)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }

    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState];
        }

        set
        {
            ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState] = value;
        }
    }
    #endregion

    #region Common Variables for Language Translation

    #region Status_All
    public string Status_All
    {
        get
        {
            return ViewState["Status_All"].ToString();
        }

        set
        {
            ViewState["Status_All"] = value;
        }
    }
    #endregion

    #region Status_Submitted
    public string Status_Submitted
    {
        get
        {
            return ViewState["Status_Submitted"].ToString();
        }

        set
        {
            ViewState["Status_Submitted"] = value;
        }
    }
    #endregion

    #region Status_Approved
    public string Status_Approved
    {
        get
        {
            return ViewState["Status_Approved"].ToString();
        }

        set
        {
            ViewState["Status_Approved"] = value;
        }
    }
    #endregion

    #region Status_Escalated
    public string Status_Escalated
    {
        get
        {
            return ViewState["Status_Escalated"].ToString();
        }

        set
        {
            ViewState["Status_Escalated"] = value;
        }
    }
    #endregion

    #region Status_Processed
    public string Status_Processed
    {
        get
        {
            return ViewState["Status_Processed"].ToString();
        }

        set
        {
            ViewState["Status_Processed"] = value;
        }
    }
    #endregion

    #region Status_Paid
    public string Status_Paid
    {
        get
        {
            return ViewState["Status_Paid"].ToString();
        }

        set
        {
            ViewState["Status_Paid"] = value;
        }
    }
    #endregion

    #region Status_RejectedByApprover
    public string Status_RejectedByApprover
    {
        get
        {
            return ViewState["Status_RejectedByApprover"].ToString();
        }

        set
        {
            ViewState["Status_RejectedByApprover"] = value;
        }
    }
    #endregion

    #region Status_RejectedByFinance
    public string Status_RejectedByFinance
    {
        get
        {
            return ViewState["Status_RejectedByFinance"].ToString();
        }

        set
        {
            ViewState["Status_RejectedByFinance"] = value;
        }
    }
    #endregion

    #region GeneralClaim
    public string GeneralClaim
    {
        get
        {
            return ViewState["GeneralClaim"].ToString();
        }

        set
        {
            ViewState["GeneralClaim"] = value;
        }
    }
    #endregion

    #region EntertainmentClaim
    public string EntertainmentClaim
    {
        get
        {
            return ViewState["EntertainmentClaim"].ToString();
        }

        set
        {
            ViewState["EntertainmentClaim"] = value;
        }
    }
    #endregion

    #region PACE&MarketingEventClaim
    public string PACEMarketingEventClaim
    {
        get
        {
            return ViewState["PACEMarketingEventClaim"].ToString();
        }

        set
        {
            ViewState["PACEMarketingEventClaim"] = value;
        }
    }
    #endregion

    #region SortByASC
    protected string SortByASC
    {
        get
        {
            return ViewState["SortByASC"].ToString();
        }

        set
        {
            ViewState["SortByASC"] = value;
        }
    }
    #endregion

    #region SortByDESC
    protected string SortByDESC
    {
        get
        {
            return ViewState["SortByDESC"].ToString();
        }

        set
        {
            ViewState["SortByDESC"] = value;
        }
    }
    #endregion

    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();        

        if (!IsPostBack)
        {
            this.CurrentPageIndex = 0;

            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();

            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView);
        } 
    }
    #endregion

    #region DropDownList_ByYear_SelectedIndexChanged
    protected void DropDownList_ByYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadData();

        DataView ResultView = new DataView(this.ResultTable);

        if (this.DropDownList_ByStatus.SelectedIndex > 0)
        {
            ResultView.RowFilter = "Status = '" + this.DropDownList_ByStatus.SelectedValue + "'";
        }

        ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
        this.BindData(ResultView);
    }
    #endregion

    #region DropDownList_SortBy_SelectedIndexChanged
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView ResultView = new DataView(this.ResultTable);

        if (this.DropDownList_ByStatus.SelectedIndex > 0)
        {
            ResultView.RowFilter = "Status = '" + this.DropDownList_ByStatus.SelectedValue + "'";
        }

        ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
        this.BindData(ResultView, this.CurrentPageIndex);
    }
    #endregion 

    #region DropDownList_SortOrder_SelectedIndexChanged
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region DropDownList_ByStatus_SelectedIndexChanged
    protected void DropDownList_ByStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region GridViewResult_PageIndexChanging
    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortOrder_SelectedIndexChanged(sender, e);
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {

        // years
        int YearStarted = 2007;
        for (int x = YearStarted; x <= DateTime.Now.Year; x++)
        {
            this.DropDownList_ByYear.Items.Add(new ListItem(x.ToString(), x.ToString()));
        }
        this.DropDownList_ByYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
        
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");

                // grid
                this.GridViewResult.Columns[0].HeaderText = CurLanguageFactory.DisplayValue("ItemDateCreated");
                this.GridViewResult.Columns[1].HeaderText = CurLanguageFactory.DisplayValue("ItemCategory");
                this.GridViewResult.Columns[2].HeaderText = CurLanguageFactory.DisplayValue("ItemClaimID");
                this.GridViewResult.Columns[3].HeaderText = CurLanguageFactory.DisplayValue("ItemPurpose");
                this.GridViewResult.Columns[4].HeaderText = CurLanguageFactory.DisplayValue("ItemClaimPeriod");
                this.GridViewResult.Columns[5].HeaderText = CurLanguageFactory.DisplayValue("ItemCurrency");
                this.GridViewResult.Columns[6].HeaderText = CurLanguageFactory.DisplayValue("ItemAmount");
                this.GridViewResult.Columns[7].HeaderText = CurLanguageFactory.DisplayValue("ItemApprovedAmount");
                this.GridViewResult.Columns[8].HeaderText = CurLanguageFactory.DisplayValue("ItemStatus");
                this.GridViewResult.Columns[9].HeaderText = CurLanguageFactory.DisplayValue("ItemHistory");

                // status
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusAll"), ""));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusSubmitted"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Submitted).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusEscalated"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Escalated).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusApproved"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Approved).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusPendingForAuthorizarion"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.PendingForAuthorize).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusPendingForFinanceVerification"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.PendingForFinanceVerification).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusVerified"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.Verified).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusHoldByFinance"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.OnHold).ToString()));
                this.DropDownList_ByStatus.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("StatusStatusRejectedByFinance"), ClaimManager.GetClaimStatusInCode(GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance).ToString()));

                // sort by
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemDateCreated"), "DateCreated"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemCategory"), "Category"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemClaimID"), "ClaimID"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemPurpose"), "Purpose"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemClaimPeriod"), "ClaimPeriod"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemCurrency"), "Currency"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemAmount"), "Amount"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemStatus"), "Status"));

                // sort order
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("SortOrderASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("SortOrderDESC"), "DESC"));

                // category
                this.GeneralClaim = CurLanguageFactory.DisplayValue("ItemGeneral");
                this.EntertainmentClaim = CurLanguageFactory.DisplayValue("ItemEntertainment");
                // Thang Nguyen added 27/03/2013
                this.PACEMarketingEventClaim = CurLanguageFactory.DisplayValue("ItemPACEMarketingEvent");

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesMyClaims.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region TranslateCategory
    protected string TranslateCategory(CategoryOfClaim ClaimCategory)
    {
        string result = this.GeneralClaim;

        switch (ClaimCategory)
        {
            case CategoryOfClaim.General:
                result = this.GeneralClaim;
                break;

            case CategoryOfClaim.Entertainment:
                result = this.EntertainmentClaim;
                break;
            case CategoryOfClaim.PACEMarketingEvent:
                result = this.PACEMarketingEventClaim;
                break;
        }

        return result;
    }

    #endregion

    #endregion

    #region Data Operation

    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ClaimHeaders ResultObj = CurClaimManager.GetClaimByStatus(
                ClaimManager.GetClaimStatusFromCode(this.DropDownList_ByStatus.SelectedValue), 
                this.CurrentPrivateSession.CurrentLoggedOnUser, Convert.ToInt32(this.DropDownList_ByYear.SelectedValue));                        

            if (ResultObj != null)
            {
                this.ResultTable = new DataTable();
                ResultTable.Columns.Add(new DataColumn("DateCreated", Type.GetType("System.DateTime")));
                ResultTable.Columns.Add(new DataColumn("DateCreatedInString", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Category", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ClaimID", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Purpose", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ClaimPeriod", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Currency", Type.GetType("System.String")));                
                ResultTable.Columns.Add(new DataColumn("Amount", Type.GetType("System.Decimal")));
                ResultTable.Columns.Add(new DataColumn("AmountInStr", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("FinalAmount", Type.GetType("System.Decimal")));
                ResultTable.Columns.Add(new DataColumn("FinalAmountInStr", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Status", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("StatusInString", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ShowHistory", Type.GetType("System.String")));

                DataRow RowObj = null;

                for (int x = 0; x < ResultObj.Count; x++)
                {
                    GonzalesClaimHeader CurClaimHeader = (GonzalesClaimHeader)ResultObj[x];

                    // draft will not be displayed
                    if (CurClaimHeader.Status != GonzalesClaimHeader.TypeOfClaimStatus.Draft)
                    {
                        RowObj = ResultTable.NewRow();

                        RowObj["DateCreated"] = ResultObj[x].CreatedOn;
                        RowObj["DateCreatedInString"] = "<a href='GonzalesMyClaimDetail.aspx?id=" + ResultObj[x].InternalID + "' class='GridViewLink'>" + SwissArmy.DateConvert(ResultObj[x].CreatedOn, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/") + "</a>";
                        RowObj["Category"] = this.TranslateCategory(CurClaimHeader.Category);
                        RowObj["ClaimID"] = ResultObj[x].ClaimID;
                        RowObj["Purpose"] = ResultObj[x].Reasons;
                        if (ResultObj[x].ProjectedOn != DateTime.MinValue)
                            RowObj["ClaimPeriod"] =ResultObj[x].ProjectedOn.Year.ToString() + " - " + ResultObj[x].ProjectedOn.Month.ToString();// ResultObj[x].ProjectedOn.ToString("MMMM") + " " + ResultObj[x].ProjectedOn.Year.ToString();
                        else
                            RowObj["ClaimPeriod"] = "";
                        RowObj["Currency"] = CurClaimHeader.LocalCurrency.DisplayName;
                        RowObj["Amount"] = ResultObj[x].TotalClaimedAmount;
                        RowObj["AmountInStr"] = ResultObj[x].TotalClaimedAmount.ToString(GonzalesGeneralVariables.CurrencyFormat);
                        RowObj["FinalAmount"] = ResultObj[x].FinalApprovedAmount;
                        RowObj["FinalAmountInStr"] = ResultObj[x].FinalApprovedAmount.ToString(GonzalesGeneralVariables.CurrencyFormat);
                        RowObj["Status"] = ClaimManager.GetClaimStatusInCode(CurClaimHeader.Status).ToString();
                        RowObj["StatusInString"] = ClaimManager.GetClaimStatusInString(CurClaimHeader.Status);

                        //Added by Agnes - for enhancement 2009
                        #region Action Log
                        string history = "";

                        LogsCollection ActionLogs = this.CurLogManager.GetActionLogByClaimID(ResultObj[x].InternalID);
                        if (ActionLogs != null)
                        {
                            StringBuilder ActionLogBuilder = new StringBuilder();

                            #region Status

                            for (int y = 0; y < ActionLogs.Count; y++)
                            {
                                ActionLogItem CurLogItem = (ActionLogItem)ActionLogs[y];
                                string Status = "";

                                switch (CurLogItem.ActionDescription)
                                {
                                    case "Claim Created":
                                        Status = "Claim created on";
                                        break;

                                    case "Claim Submitted":
                                        Status = "Claim submitted on";
                                        break;

                                    case "Claim Saved":
                                        Status = "Claim saved on";
                                        break;

                                    case "Claim Approved":
                                        Status = "Claim approved on";
                                        break;

                                    case "Claim Authorized":
                                        Status = "Claim authorized on";
                                        break;

                                    case "Claim Verified":
                                        Status = "Claim processed on";
                                        break;

                                    case "Claim Finance Approved":
                                        Status = "Claim verified (Finance verifier) on";
                                        break;

                                    case "Claim Rejected By Approver":
                                        Status = "Claim rejected (Approver) on";
                                        break;

                                    case "Claim Rejected By Authorizer":
                                        Status = "Claim rejected (Authorizer) on";
                                        break;

                                    case "Claim Rejected By FinanceAdmin":
                                        Status = "Claim rejected (Finance Admin) on";
                                        break;

                                    case "Claim Rejected By FinanceApprover":
                                        Status = "Claim rejected (Finance verifier) on";
                                        break;

                                        //added by mei chze on 23072012
                                    case "Claim Status Change By System Admin":
                                        Status = "Claim Status Change on";
                                        break;
                                }

                            #endregion

                                ActionLogBuilder.AppendLine(Status + " " + SwissArmy.DateConvert(CurLogItem.LogDate, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", true) + " by " + CurLogItem.LoggedBy.DisplayName);
                                history += Status + " " + SwissArmy.DateConvert(CurLogItem.LogDate, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", true) + " by " + CurLogItem.LoggedBy.DisplayName + "\\r\\n";

                            }
                            RowObj["ShowHistory"] = "<a href='#' onclick=\"alert('" +history+ "');\"><img border='0' src='" + GonzalesImages.Icon_Detail + "'></a>";

                        }
                        else
                        {
                            string LogID = SwissArmy.UniqueID();
                            this.CurLogManager.LogError(LogID, "[GonzalesMyClaimDetail.LoadData] : Failed at this.CurLogManager.GetActionLogByClaimID('" + ResultObj[x].ClaimID + "') : " + this.CurLogManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                        }

                        #endregion

                       
                        ResultTable.Rows.Add(RowObj);
                    }
                }

            }
        }
    }

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

}
